<?php

/**
 * Radcodes - SocialEngine Module
 *
 * @category   Application_Extensions
 * @package    Story
 * @copyright  Copyright (c) 2009-2010 Radcodes LLC (http://www.radcodes.com)
 * @license    http://www.radcodes.com/license/
 * @version    $Id$
 * @author     Vincent Van <vincent@radcodes.com>
 */


class Story_Model_DbTable_Chapters extends Engine_Db_Table
{
    protected $_rowClass = 'Story_Model_Chapter';

    public function getChapterSelect($params = array())
    {
        $select = $this->select();

        if (isset($params['story_id'])) {
            $select->where("story_id = ?", $params['story_id']);
        }

        if (isset($params['public'])) {
            $select->where("public = ?", $params['public']);
        }

        if (!empty($params['order'])) {
            $select->order($params['order']);
        }

        if (isset($params['chapter_id'])) {
        	$select->where("chapter_id = ?", $params['chapter_id']);
        }       

        return $select;
    }

    public function getChapters($story_id)
    {
        if (empty($story_id)) {
            return null;
        }

        $params = array('story_id' => $story_id, 'public' => 1);
        $params = array_merge(array('order' => 'order'), $params);
        $select = $this->getChapterSelect($params);
		
        return $this->fetchAll($select);
    }
    
    public function addChapter($params = array()){
    	if(!$params['story_id']){
    		return;
    	}
    	
    	$viewer = Engine_Api::_()->user()->getViewer();
    	$viewer_id = $viewer->getIdentity();
    	$item = $this->createRow();
    	$item->setFromArray($params);
    	$item->user_id = $viewer_id;
    	$item->order = $this->getLastOrder($params['story_id']);
    	
    	$item->save();
    	
    	return $item;
    }
    
    public function editChapter($params = array()){
        if(!$params['story_id']){
            return;
        }
        $select = $this->select();
        $select->where("chapter_id=?", $params['chapter_id']);
        $item = $this->fetchRow($select);
        if ($item) {
            $item->setFromArray($params);
            $item->save();
        }
        return $item;
    }

    public function getLastOrder($story_id){
    	if(empty($story_id)){
    		return 0;
    	}
    	$rName = $this->info('name'); 
    	$select = $this->select();
    	$select->where("story_id = ?", $story_id);
    	$select->order("{$rName}.order DESC");
    	$select->limit(1);
    	
    	$item = $this->fetchRow($select);
    	$order = 1;
    	if($item){
    		$order = $item->order + 1;
    	}
    	return $order;
    }
    
    public function getLastChapter($story_id){
    	if(empty($story_id)){
    		return 0;
    	}
    	$rName = $this->info('name'); 
    	$select = $this->select();
    	$select->where("story_id = ?", $story_id);
    	$select->order("{$rName}.order DESC");
    	$select->limit(1);
    	 
    	$item = $this->fetchRow($select);
    	
    	return $item;
    }
    
    public function getLastChapterThread($story_id){
        if(empty($story_id)){
            return 0;
        }
        $rName = $this->info('name'); 
        $select = $this->select();
        $select->where("story_id = ?", $story_id);
        $select->where("public = ?", 1);
        $select->order("{$rName}.order DESC");
        $select->limit(1);
         
        $item = $this->fetchRow($select);
        
        return $item;
    }

    public function getNextChapter($story_id, $order){
    	if(empty($story_id) || empty($order)) return NULL;
    	$rName = $this->info('name');
    	$select = $this->select();
    	$select->where("story_id = ?", $story_id);
    	$select->where("{$rName}.order > ?", $order);    	
    	$select->order("{$rName}.order DESC");
    	$select->limit(1);
    	
    	$item = $this->fetchRow($select);
    	return $item;
    }
    
    public function getPrevChapter($story_id, $order){
    	if(empty($story_id) || empty($order)) return NULL;
    	$rName = $this->info('name');
    	$select = $this->select();
    	$select->where("story_id = ?", $story_id);
    	$select->where("{$rName}.order < ?", $order);
    	$select->order("{$rName}.order DESC");
    	$select->limit(1);
    	$item = $this->fetchRow($select);
    	return $item;
    }
    
    public function getFirstChapter($story_id){
    	if(empty($story_id)) return NULL;
    	$rName = $this->info('name'); 
    	$select = $this->select();
    	$select->where("story_id = ?", $story_id);
    	
    	$select->order("{$rName}.order");
    	$select->limit(1);
    	
    	$item = $this->fetchRow($select);
    	return $item;
    }
}
